<template>
  <main class="diy-page">
    <div class="container">
      <!-- 收藏成功提示 -->
      <div class="favorite-toast" v-if="showFavoriteToast">
        <div class="toast-content">
          <span class="toast-icon">{{
            favoriteToastType === "add" ? "❤️" : "🤍"
          }}</span>
          <span class="toast-message">{{
            favoriteToastType === "add" ? "已添加到我的收藏" : "已从收藏中移除"
          }}</span>
        </div>
      </div>
      <!-- 热门和新出花束推荐 -->
      <div class="recommendations">
        <div class="recommendation-tabs">
          <button
            class="tab-btn"
            :class="{ active: activeRecommendTab === 'popular' }"
            @click="activeRecommendTab = 'popular'"
          >
            热门花束
          </button>
          <button
            class="tab-btn"
            :class="{ active: activeRecommendTab === 'new' }"
            @click="activeRecommendTab = 'new'"
          >
            新品上市
          </button>
          <button
            class="tab-btn"
            :class="{ active: activeRecommendTab === 'nearby' }"
            @click="activeRecommendTab = 'nearby'"
          >
            附近花店样品
          </button>
        </div>

        <div class="recommendation-content">
          <div class="bouquet-cards">
            <div
              v-for="bouquet in filteredRecommendedBouquets"
              :key="bouquet.id"
              class="bouquet-card"
            >
              <div class="bouquet-image">
                <img :src="bouquet.image" :alt="bouquet.name" />
                <div class="bouquet-badge" v-if="bouquet.isNew">新品</div>
                <div class="bouquet-badge popular" v-if="bouquet.isPopular">
                  热门
                </div>
                <div class="bouquet-badge nearby" v-if="bouquet.isNearby">
                  附近
                </div>
                <!-- 添加收藏按钮 -->
                <button
                  class="favorite-btn recommended-favorite-btn"
                  @click.stop="toggleFavorite(bouquet, 'recommended')"
                  :class="{ favorited: isFavoritedRecommended(bouquet.id) }"
                >
                  <span v-if="isFavoritedRecommended(bouquet.id)">❤️</span>
                  <span v-else>🤍</span>
                </button>
              </div>
              <div class="bouquet-info">
                <h4 class="bouquet-name">{{ bouquet.name }}</h4>
                <button class="select-bouquet-btn" @click="addToCart(bouquet)">
                  添加到购物车
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </main>
</template>

<script>
export default {
  name: "DiyPopular",
  data() {
    return {
      // 收藏相关
      favorites: [],
      showFavoriteToast: false,
      favoriteToastType: "add", // 'add' 或 'remove'

      // 搜索和推荐
      activeRecommendTab: "popular",
      recommendedBouquets: [
        {
          id: 1,
          name: "浪漫粉玫瑰",
          price: 199,
          image: "@/assets/images/bouquet-1.jpg",
          isPopular: true,
          isNew: false,
          isNearby: false,
          flowers: [
            { id: 1, name: "粉玫瑰", quantity: 11 },
            { id: 5, name: "满天星", quantity: 3 },
            { id: 6, name: "绿叶", quantity: 5 },
          ],
          packaging: 2,
        },
        {
          id: 2,
          name: "向日葵阳光",
          price: 168,
          image: "@/assets/images/bouquet-2.jpg",
          isPopular: true,
          isNew: false,
          isNearby: false,
          flowers: [
            { id: 3, name: "向日葵", quantity: 5 },
            { id: 6, name: "绿叶", quantity: 4 },
          ],
          packaging: 1,
        },
        {
          id: 3,
          name: "百合之恋",
          price: 258,
          image: "@/assets/images/bouquet-3.jpg",
          isPopular: false,
          isNew: false,
          isNearby: true,
          flowers: [
            { id: 4, name: "百合", quantity: 6 },
            { id: 5, name: "满天星", quantity: 3 },
            { id: 6, name: "绿叶", quantity: 4 },
          ],
          packaging: 3,
        },
        {
          id: 4,
          name: "混合花束",
          price: 288,
          image: "@/assets/images/bouquet-4.jpg",
          isPopular: false,
          isNew: false,
          isNearby: true,
          flowers: [
            { id: 1, name: "粉玫瑰", quantity: 3 },
            { id: 2, name: "红玫瑰", quantity: 3 },
            { id: 4, name: "百合", quantity: 2 },
            { id: 5, name: "满天星", quantity: 2 },
            { id: 6, name: "绿叶", quantity: 3 },
          ],
          packaging: 2,
        },
        {
          id: 5,
          name: "紫色洋桔梗",
          price: 228,
          image: "@/assets/images/bouquet-5.jpg",
          isPopular: false,
          isNew: true,
          isNearby: false,
          flowers: [
            { id: 7, name: "洋桔梗", quantity: 8 },
            { id: 5, name: "满天星", quantity: 3 },
            { id: 6, name: "绿叶", quantity: 4 },
          ],
          packaging: 2,
        },
        {
          id: 6,
          name: "蓝色绣球花",
          price: 268,
          image: "@/assets/images/bouquet-6.jpg",
          isPopular: false,
          isNew: true,
          isNearby: false,
          flowers: [
            { id: 8, name: "绣球花", quantity: 3 },
            { id: 6, name: "绿叶", quantity: 5 },
          ],
          packaging: 3,
        },
        {
          id: 7,
          name: "附近花店特选",
          price: 188,
          image: "@/assets/images/bouquet-7.jpg",
          isPopular: false,
          isNew: false,
          isNearby: true,
          flowers: [
            { id: 2, name: "红玫瑰", quantity: 7 },
            { id: 5, name: "满天星", quantity: 3 },
            { id: 6, name: "绿叶", quantity: 4 },
          ],
          packaging: 1,
          shopName: "花艺坊",
          distance: "0.8km",
        },
        {
          id: 8,
          name: "附近花店推荐",
          price: 298,
          image: "@/assets/images/bouquet-8.jpg",
          isPopular: false,
          isNew: false,
          isNearby: true,
          flowers: [
            { id: 7, name: "洋桔梗", quantity: 5 },
            { id: 8, name: "绣球花", quantity: 2 },
            { id: 6, name: "绿叶", quantity: 3 },
          ],
          packaging: 2,
          shopName: "花之语",
          distance: "1.2km",
        },
      ],
    };
  },
  computed: {
    filteredRecommendedBouquets() {
      if (this.activeRecommendTab === "popular") {
        return this.recommendedBouquets.filter((bouquet) => bouquet.isPopular);
      } else if (this.activeRecommendTab === "new") {
        return this.recommendedBouquets.filter((bouquet) => bouquet.isNew);
      } else if (this.activeRecommendTab === "nearby") {
        return this.recommendedBouquets.filter((bouquet) => bouquet.isNearby);
      }
      return this.recommendedBouquets;
    },
  },
  created() {
    // 加载收藏数据
    this.loadFavorites();
  },
  methods: {
    addToCart(bouquet) {
      // 构建花束数据
      const bouquetData = {
        name: bouquet.name || "自定义花束",
        price: bouquet.price,
        image: bouquet.image,
        flowers: [...bouquet.flowers],
        type: "custom",
      };

      // 使用自定义事件发送数据到购物车组件
      const event = new CustomEvent("add-to-cart", {
        detail: bouquetData,
        bubbles: true,
      });
      window.dispatchEvent(event);
    },
    // 收藏相关方法
    loadFavorites() {
      const storedFavorites = localStorage.getItem("userFavorites");
      if (storedFavorites) {
        this.favorites = JSON.parse(storedFavorites);
      }
    },
    saveFavorites() {
      localStorage.setItem("userFavorites", JSON.stringify(this.favorites));
    },
    isFavoritedRecommended(bouquetId) {
      // 检查推荐花束是否已收藏
      return this.favorites.some(
        (fav) => fav.type === "recommended" && fav.id === bouquetId
      );
    },
    toggleFavorite(type, sourceType) {
      if (sourceType === "recommended" && type) {
        // 收藏推荐花束
        const bouquet = type; // 这里type实际上是bouquet对象
        const existingIndex = this.favorites.findIndex(
          (fav) => fav.type === "recommended" && fav.id === bouquet.id
        );

        if (existingIndex !== -1) {
          // 移除收藏
          this.favorites.splice(existingIndex, 1);
          this.showFavoriteToast = true;
          this.favoriteToastType = "remove";
        } else {
          // 添加收藏
          const recommendedBouquet = {
            id: bouquet.id,
            type: "recommended",
            name: bouquet.name,
            price: bouquet.price,
            image: bouquet.image,
            flowers: [...bouquet.flowers],
            packaging: bouquet.packaging,
            date: new Date().toISOString(),
          };

          this.favorites.push(recommendedBouquet);
          this.showFavoriteToast = true;
          this.favoriteToastType = "add";
        }

        this.saveFavorites();
        this.hideToastAfterDelay();
        return;
      }
    },
    hideToastAfterDelay() {
      setTimeout(() => {
        this.showFavoriteToast = false;
      }, 2000);
    },
  },
};
</script>

<style scoped>
.diy-page {
  padding-top: 30px; /* 为固定头部留出空间 */
  background: linear-gradient(135deg, #d693a1, #e2f5ff);
  min-height: 90vh;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
  height: 600px;
}

/* 搜索和推荐区域 */
.recommendations {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 1.5rem;
  margin-bottom: 2rem;
  height: 100%;
  overflow: auto;
}

.recommendation-tabs {
  display: flex;
  border-bottom: 1px solid #eee;
  margin-bottom: 1rem;
}

.recommendation-tabs .tab-btn {
  padding: 0.8rem 1.5rem;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  color: #666;
  transition: all 0.3s;
  border-bottom: 2px solid transparent;
}

.recommendation-tabs .tab-btn:hover {
  color: #e75480;
}

.recommendation-tabs .tab-btn.active {
  color: #e75480;
  border-bottom-color: #e75480;
  font-weight: 500;
}

.bouquet-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
}

.bouquet-card {
  background-color: #f9f9f9;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
  cursor: pointer;
}

.bouquet-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.bouquet-image {
  height: 150px;
  overflow: hidden;
  position: relative;
}

.bouquet-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.bouquet-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #e75480;
  color: white;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
}

.bouquet-badge.popular {
  background-color: #f5a623;
}

.bouquet-badge.nearby {
  background-color: #4a90e2;
}

.bouquet-info {
  padding: 1rem;
  text-align: center;
}

.bouquet-name {
  font-size: 1rem;
  margin-bottom: 0.3rem;
  color: #333;
}

.bouquet-price {
  color: #e75480;
  font-weight: 500;
  margin-bottom: 0.8rem;
}

.select-bouquet-btn {
  width: 100%;
  padding: 0.5rem;
  background-color: #e75480;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
  font-size: 0.9rem;
}

.select-bouquet-btn:hover {
  background-color: #d64d7b;
}

/* 收藏按钮 */
.favorite-btn {
  position: absolute;
  bottom: 10px;
  right: 10px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.8);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  cursor: pointer;
  transition: all 0.3s;
  z-index: 10;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.favorite-btn:hover {
  background-color: white;
  transform: scale(1.1);
}

.favorite-btn.favorited {
  background-color: rgba(255, 255, 255, 0.9);
}

/* 收藏提示 */
.favorite-toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 0.8rem 1.5rem;
  border-radius: 30px;
  z-index: 1000;
  animation: fadeInOut 2s ease-in-out;
}

.toast-content {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.toast-icon {
  font-size: 1.2rem;
}

.toast-message {
  font-size: 0.9rem;
}

@keyframes fadeInOut {
  0% {
    opacity: 0;
    transform: translate(-50%, 20px);
  }
  20% {
    opacity: 1;
    transform: translate(-50%, 0);
  }
  80% {
    opacity: 1;
    transform: translate(-50%, 0);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -20px);
  }
}

.preview-info {
  padding: 1rem;
}

.preview-title {
  font-size: 1.2rem;
  margin-bottom: 0.3rem;
  color: #333;
}

.preview-price {
  font-size: 1.3rem;
  font-weight: bold;
  color: #e75480;
  margin-bottom: 0.8rem;
}

.preview-details {
  color: #666;
  font-size: 0.9rem;
}

.preview-details p {
  margin-bottom: 0.3rem;
}

.action-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 0.8rem;
  padding: 0 1rem 1rem;
}

.btn-outline {
  padding: 0.7rem 0;
  background: none;
  border: 1px solid #e75480;
  color: #e75480;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  text-align: center;
}

.btn-outline:hover {
  background-color: #f8e1e7;
}

.btn-primary {
  padding: 0.7rem 0;
  background-color: #e75480;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
  text-align: center;
}

.btn-primary:hover {
  background-color: #d64d7b;
}

.tab-btn {
  flex: 1;
  padding: 0.8rem;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.9rem;
  color: #666;
  transition: all 0.3s;
  border-bottom: 2px solid transparent;
}

.tab-btn:hover {
  color: #e75480;
}

.tab-btn.active {
  color: #e75480;
  border-bottom-color: #e75480;
  font-weight: 500;
}

/* 添加推荐花束卡片收藏按钮样式 */
.recommended-favorite-btn {
  bottom: 10px;
  top: auto;
}
</style>